<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle">
      <span v-if="!address.fullLocation" class="placeholder"
        >请选择配送地址</span
      >
      <span v-else class="value">{{ address.fullLocation }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="isShow">
      <div class="loading" v-if="loading"></div>
      <template v-else>
        <span
          class="ellipsis"
          v-for="item in list"
          :key="item.code"
          @click="getAddress(item)"
        >
          {{ item.name }}
        </span>
      </template>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
export default {
  name: 'XtxCity',
  setup (props, { emit }) {
    const isShow = ref(false)
    // 城市数据
    const cityList = ref([])
    const getCityData = async () => {
      // 判断是否需要请求
      if (!window.cityData) {
        // 获取城市信息
        const { data } = await axios(
          'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
        )
        // 存储数据
        if (data) {
          window.cityData = data
        }
      }
      // 导出数据
      return window.cityData
    }

    // loading图
    const loading = ref(false)
    // 显示
    const show = async () => {
      // 城市显示
      isShow.value = true
      // loading图显示
      loading.value = true
      // 获取数据
      const res = await getCityData()
      // loading图隐藏
      loading.value = false
      // 赋值
      cityList.value = res
      // 清空之前选中的城市
      for (const key in address) {
        address[key] = ''
      }
    }
    // 隐藏
    const hide = () => {
      // 城市隐藏
      isShow.value = false
    }
    // 显示与隐藏的切换
    const toggle = () => {
      isShow.value === true ? hide() : show()
    }

    // 选中的城市数据收集
    const address = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })
    // 点击获取城市
    const getAddress = (theItem) => {
      // 省
      if (theItem.level === 0) {
        address.provinceCode = theItem.code
        address.provinceName = theItem.name
      } else if (theItem.level === 1) {
        // 市
        address.cityCode = theItem.code
        address.cityName = theItem.name
      } else if (theItem.level === 2) {
        // 区
        address.countyCode = theItem.code
        address.countyName = theItem.name
        // 整合地址
        address.fullLocation = `${address.provinceName} ${address.cityName} ${address.countyName}`
        hide()
        // 将地址对象导出
        emit('change', address)
      }
    }

    // 计算当前城市等级，切换渲染列表
    const list = computed(() => {
      let temp = cityList.value
      // 判断是否有省
      if (address.provinceCode) {
        temp = temp.find((item) => item.code === address.provinceCode).areaList
      }
      // 判断是否有市
      if (address.cityCode) {
        temp = temp.find((item) => item.code === address.cityCode).areaList
      }

      return temp
    })

    // 在target外点击
    const target = ref(null)
    onClickOutside(target, () => {
      isShow.value = false
    })

    return {
      isShow,
      toggle,
      target,
      cityList,
      loading,
      getAddress,
      list,
      address
    }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
